package pl.edu.agh.uddiProxy.types;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.AccessType;

@Table(name = "serviceDynamicParameters",
	    uniqueConstraints = {@UniqueConstraint(columnNames={"tModelKey"})}
)
@AccessType("field")
@javax.persistence.Entity
public class ServiceDynamicParameters extends Entity {
	@Column(name = "tModelKey", nullable = false)
	private String tModelKey;
	@OneToMany(targetEntity = ParameterValue.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @MapKey(name="name")
    @JoinTable(name = "serviceDynamicParameters_map", joinColumns = @JoinColumn(name = "param_id"))
	private Map<String, ParameterValue> map;
	
	public ServiceDynamicParameters() {
		map = new HashMap<String, ParameterValue>();
	}
	
	public ServiceDynamicParameters(String modelKey) {
		this();
		tModelKey = modelKey;
	}

	public String getTModelKey() {
		return tModelKey;
	}

	public void setTModelKey(String modelKey) {
		tModelKey = modelKey;
	}

	public Map<String, ParameterValue> getMap() {
		return map;
	}

	public void setMap(Map<String, ParameterValue> map) {
		this.map = map;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((tModelKey == null) ? 0 : tModelKey.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ServiceDynamicParameters other = (ServiceDynamicParameters) obj;
		if (tModelKey == null) {
			if (other.tModelKey != null)
				return false;
		} else if (!tModelKey.equals(other.tModelKey))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return tModelKey + " " + map;
	}
}
